/* ************************************************************************

   Copyright: Prince Samuel

   License: MPL

   Authors: Prince Samuel

************************************************************************ */


/* ************************************************************************

#asset(ohmweb/*)

************************************************************************ */

/**
 * This is the main application class 
 */

/* ************************************************************************

#asset(qx/icon/${qx.icontheme}/22/actions/view-refresh.png)

************************************************************************ */

qx.Class.define("ohmweb.Application",
{
  extend : qx.application.Standalone,



  /*
  *****************************************************************************
     MEMBERS
  *****************************************************************************
  */

  members :
  {
    main : function()
    {
      // Call super class
      this.base(arguments);

      //qx.log.appender.Native;

      var service = new ohmweb.DataService();
      var main = new ohmweb.MainWindow();

      main.addListener("reload", function() {
          service.refreshData();
      }, this);

      main.addListener("interval", function() {
          service.refreshData();
      }, this);

      this.getRoot().add(main, {edge : 0});

      service.bind("tree", main.getTree(), "model");

      //delegate for custom treatment of tree.
      main.getTree().setDelegate(this);

      service.addListener("dataLoaded", function() {
          var t = main.getTree();
          this.expandAll(t, t.getModel(), this, main.getNodeInfo());
      }, this);

      //load JSON data on start.
      service.refreshData();

    },

    expandAll : function(tree, node, baap, nodeInfo) {
        node.getChildren().forEach(function(o) {
            if (nodeInfo[o.getId()] == null || nodeInfo[o.getId()])
                tree.openNode(o);
            baap.expandAll(tree, o, baap, nodeInfo);
        });
    },

    // delegate implementation
    bindItem : function(controller, item, id) {
                 controller.bindDefaultProperties(item, id);

                 controller.bindProperty("Min", "min", null, item, id);
                 controller.bindProperty("Value", "value", null, item, id);
                 controller.bindProperty("Max", "max", null, item, id);
               },
    createItem : function() {
       //our custom TreeFolder
       return new ohmweb.TreeColumn();
                 }
  }
});
